package cn.est.utils;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @ClassName HttpClientUtil
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/4/8 9:07
 * @Version 1.0
 **/
public class HttpClientUtil {

    /**
     * 执行get请求
     *
     * @param url 请求url
     * @return 若null则调用失败，否则返回接口数据
     */
    public static String doGet(String url) {
        //获得http客户端
        CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
        //创建get请求
        HttpGet httpGet = new HttpGet(url);

        //初始化响应模型
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            //配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    //连接超时时间(毫秒)
                    .setConnectTimeout(5000)
                    //请求超时时间(毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(毫秒)
                    .setSocketTimeout(5000)
                    //是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();
            //注入配置
            httpGet.setConfig(requestConfig);
            //由客户端执行(发送)get请求
            closeableHttpResponse = closeableHttpClient.execute(httpGet);
            //从http响应模型中获取状态码
            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            //若http响应不是200，返回null
            if (statusCode != 200) {
                return null;
            }
            //从http响应模型中获取响应实体，调用工具类解析实体得到接口返回的数据
            String responseContent = EntityUtils.toString(closeableHttpResponse.getEntity());
            //返回结果
            return responseContent;
        } catch (ClientProtocolException e) {
            return null;
        } catch (IOException e) {
            return null;
        } finally {
            try {
                //释放资源
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
                if (closeableHttpResponse != null) {
                    closeableHttpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 执行post请求
     *
     * @param url        请求url
     * @param objectJson json参数
     * @return 若null则调用失败，否则返回接口数据
     */
    public static String doPost(String url, String objectJson) {
        //获得http客户端
        CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
        //创建post请求
        HttpPost httpPost = new HttpPost(url);
        //将json转为http实体
        StringEntity stringEntity = new StringEntity(objectJson, "UTF-8");
        //将http实体放入post请求体中
        httpPost.setEntity(stringEntity);
        //设置请求头
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        //初始化响应模型
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            //配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    //连接超时时间(毫秒)
                    .setConnectTimeout(5000)
                    //请求超时时间(毫秒)
                    .setConnectionRequestTimeout(5000)
                    //socket读写超时时间(毫秒)
                    .setSocketTimeout(5000)
                    //是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();
            //注入配置
            httpPost.setConfig(requestConfig);
            //由客户端执行(发送)get请求
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            //从http响应模型中获取状态码
            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            //若http响应不是200，返回null
            if (statusCode != 200) {
                return null;
            }
            //从http响应模型中获取响应实体，调用工具类解析实体得到接口返回的数据
            String responseContent = EntityUtils.toString(closeableHttpResponse.getEntity());
            //返回结果
            return responseContent;
        } catch (ClientProtocolException e) {
            return null;
        } catch (IOException e) {
            return null;
        } finally {
            try {
                //释放资源
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
                if (closeableHttpResponse != null) {
                    closeableHttpResponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}